<?php
class ContactUs extends Page {
    public static $db = array(
    	'Mailto' => 'Varchar(100)',
        'SubmitText' => 'Text'
    );

    public static $has_one = array();
    function getCMSFields() {
		$fields = parent::getCMSFields();

		$fields->addFieldToTab("Root.Content.OnSubmission", new TextField('Mailto', 'Email submissions to'));
		$fields->addFieldToTab("Root.Content.OnSubmission", new TextareaField('SubmitText', 'Text on Submission'));

		return $fields;
	}
}

class ContactUs_Controller extends Page_Controller {

	function ContactForm(){
		$fields = new FieldSet(
			new TextField("fname","First Name",""),
			new TextField("lname","Last Name",""),
			new OptionsetField("male","", array("Mr."=>"Mr.","Mrs."=>"Mrs."),"Mr."),
			new TextField("address","Address",""),
			new TextField("city","City",""),
			new TextField("district","District",""),
			new TextField("phone","Phone",""),
			new DropdownField(
                 $name = "country",
                 $title = "Country",
                 $source = Geoip::getCountryDropDown(),
                 $value = Geoip::visitor_country()
            ),
			new EmailField("email","Email",""),
			new TextareaField("comments","Comments","")
		);

		$validator = new RequiredFields('fname', 'Email', 'Comments');

		$actions = new FieldSet(
			new ResetFormAction('ClearAction','Clear'),
        	new FormAction('SendContactForm', 'Submit')
      	);

      	return new Form($this, 'BrowserPollForm', $fields, $actions);

	}

	function SendContactForm($data) {

	 	//Set data
		$From = $data['email'];
		$To = $this->Mailto;
		$Subject = "Website Contact message";
		$email = new Email($From, $To, $Subject);
		//set template
		$email->setTemplate('ContactEmail');
		//populate template
		$email->populateTemplate($data);
		//send mail
		$email->send();
	  	//return to submitted message
		Director::redirect(Director::baseURL(). $this->URLSegment . "/?success=1");
	}

	public function Success()
	{
		return isset($_REQUEST['success']) && $_REQUEST['success'] == "1";
	}


}
?>
